package org.primefaces.model.chart.flow.step;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import org.primefaces.model.chart.flow.FlowStep;
import org.primefaces.util.ArrayUtils;

public class FigureStep extends FlowStep {

	private final static Logger logger = Logger.getLogger(FigureStep.class
			.getName());

	public static final String[] FIGURE_TYPE_ITEMS = new String[] {
			"Rectangular", "Circle", "Concentric" };

	public static final String TYPE_NAME = "figure";

	public static final String FIGURE_TYPE = "figureType";

	public static final String[] BPM_STEP_ATTRIBUTE = ArrayUtils.concat(
			STEP_ATTRIBUTE, new String[] { FIGURE_TYPE });

	private String figureType;

	@Override
	public String getType() {
		return FlowStep.SYS_FLOW_STEP_TYPE_PREFIX + TYPE_NAME;
	}

	public String getFigureType() {
		return figureType;
	}

	public void setFigureType(String figureType) {
		for (String t : FIGURE_TYPE_ITEMS) {
			if (t.equals(figureType)) {
				this.figureType = figureType;
				return;
			}
		}
		logger.log(Level.SEVERE, "Error in set figure type.");
	}

	@Override
	public JSONObject toJSON() {
		try {
			// 添加父类属性
			JSONObject jo = super.toJSON();

			if (this.hasAttribute(FIGURE_TYPE))
				jo.put(FIGURE_TYPE, this.getAttribute(FIGURE_TYPE));

			return jo;
		} catch (JSONException e) {
			logger.log(Level.SEVERE, "Error in transfer to json object.");
		}
		return null;
	}

	@Override
	public void construct(JSONObject json) {
		try {
			super.construct(json);

			if (json.has(FIGURE_TYPE))
				addAttribute(FIGURE_TYPE, json.getString(FIGURE_TYPE));

		} catch (JSONException e) {
			logger.log(Level.SEVERE, "Error in transfer from json object.");
		}
	}

}
